<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Chanels;
use Illuminate\Http\Request;

class ChanelController extends Controller {

    public function index(){

        $lists = Chanels::orderBy('created_at', 'asc')
            ->paginate(10);
        $this->stateToText($lists,[
            'status' => Chanels::STATUS_TEXT,
            'target' => Chanels::TARGET_TEXT,
        ]);
        return $this->writeJson(0,$lists);
    }

    public function update(Request $request)
    {
        $data = $request->all();
        if($data['id']){
            $info = Chanels::findOrFail($data['id']);
            $info->update($data);
            return $this->writeJson(0,null,'修改导航信息成功');
        }else{
            unset($data['id']);
            Chanels::create($data);
            return $this->writeJson(0,null,'新增导航信息成功');
        }
    }


    public function destroy(Request $request){
        $id = (int)$request->get('id',0);
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        Chanels::destroy($id);
        return $this->writeJson(0);
    }

}
